package es.udc.emarketapp.http.controller.actions.categories;

import es.udc.emarketapp.emarket.model.categoryFacade.CategoryFacade;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

import es.udc.emarketapp.emarket.model.categoryFacade.vo.CategoryChunkVO;
import es.udc.emarketapp.http.view.actionForms.SelectCategoryForm;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.struts.action.DefaultAction;

public class FWDSelectCategoryAction extends DefaultAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String FAILURE = "failure";

    @Override
    public ActionForward doExecute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException, InternalErrorException {

        //Aqui se hace la consulta para llenar el combo de categorias principales
        try {

            //ProductFacade productFacade = new ProductFacade();
            CategoryFacade categoryFacade = new CategoryFacade();

            CategoryChunkVO categoryChunkVO = categoryFacade.getCategorySons(1, 1, 100);
            //productFacade.getCategorySons(1, 1, 100);

            request.setAttribute("categories",
                    categoryChunkVO.getCategoryVOs());

            if (form != null) {

                SelectCategoryForm selectForm = (SelectCategoryForm) form;
                String action = null;
                action = selectForm.getAction();
                Long idProd = selectForm.getIdProd();
                if (action != null) {
                    request.setAttribute("action", action);
                    request.setAttribute("idProd", idProd);
                }
            }

            return mapping.findForward(SUCCESS);

        } catch (Exception e) {
            return mapping.findForward(FAILURE);
        }
    }
}
